// 获取 mock对象
const Mock = require("mockjs");
// 获取 random对象，随机生成各种数据
const Random = Mock.Random;
// 定义默认域名，可随意写
const domain = "http://mockjs.com/api";
// 返回的状态码
const code = 200;

// 随机生成文章数据
const postData = req => {
  // 请求体，用于获取参数
  console.log(req);

  let posts = [];

  for (let i = 0; i < 10; i++) {
    let post = {
      title: Random.csentence(10, 25),
      icon: Random.dataImage("250x250", "文章icon"),
      author: Random.cname(),
      date: Random.date() + " " + Random.time()
    };

    posts.push(post);
  }

  // 返回状态码和 posts
  return {
    code,
    posts
  };
};

const testData = Mock.mock({
  status: 200,
  msg: "success",
  "stuData|10-20": [
    {
      "name|1-3": /[a-z][A-Z]/,
      "age|+1": 10,
      "gender|1": true
    }
  ]
});

const searchQuestionData = Mock.mock({
    code: 200,
    msg: "success",
    "data|10-20": [
      {
        "id": /\d{5,10}/,
        "subject|1-8": 1,
        "kind|1-5": 1,
        "stage|1": [
            "小学一年级",
            "小学三年级",
            "小学六年级",
            "初中一年级",
            "初中三年级",
            "高中一年级",
            "高中三年级",
            "大学三年级",
            "高中三年级",
        ],
        "gender|1": true,
        "price|1-30.1":2.5,
        "detail": '细节',
        "desc": '简介',
        "difficulty|0-4.1": 1.1 
      }
    ]
});

const searchData = Mock.mock({
    code: 200,
    msg: "success",
    "data|10-20": [
      {
        "id": /\d{5,10}/,
        "subject|1-8": 1,
        "kind|1-5": 1,
        "stage|1": [
            "小学一年级",
            "小学三年级",
            "小学六年级",
            "初中一年级",
            "初中三年级",
            "高中一年级",
            "高中三年级",
            "大学三年级",
            "高中三年级",
        ],
        "gender|1": true,
        "price|1-30.1":2.5,
        "detail": '细节',
        "desc": '简介',
        "difficulty|0-4.1": 1.1 
      }
    ]
  });

const searchPaperData = Mock.mock({
    code: 200,
    msg: "success",
    "data|10-20": [
        {
            "name": "无名试卷",
            "kind|1-4": 1,
            "grade|1-3": 1,
            "maximumScore|1-200": 1,
            "secret|1-4": 1,
            "price|1-200.1": 1.1,
            "remark": "@ctitle(10)",
            "sortSwitch|1": false,
            "detail": ''      
        }
    ]
});

// 定义请求连接，类型，还有返回数据
Mock.mock(`${domain}/posts`, "get", postData);
Mock.mock(`${domain}/search`, "post", searchData);
Mock.mock(`${domain}/searchQuestion`, "post", searchQuestionData);
Mock.mock(`${domain}/searchPaper`, "post", searchPaperData);
Mock.setup({
  timeout: "200-600"
});

export {domain}
